package com.dx_society.controller;

import com.dx_society.domain.Activities;
import com.dx_society.domain.ActivityComment;
import com.dx_society.dto.AcMDTO;
import com.dx_society.dto.Result;
import com.dx_society.dto.SoMDTO;
import com.dx_society.service.*;
import com.dx_society.utils.UserHolder;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// 社长管理
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/manager")
public class ManagerController {
    @Resource
    private SocietiesMemberService societiesMemberService;
    @Resource
    private SocietiesService societiesService;
    @Resource
    private SocietyNoticeService societyNoticeService;
    @Resource
    private ActivitiesService activitiesService;
    @Resource
    private ActivityCommentService activityCommentService;
    /**
     * 查询我的社团信息
     * @return 社团信息
     */
    @GetMapping(value = "/mySociety/info")
    public Result queryMySociety() {
        return societiesService.querySocietiesDetailByMasterId();
    }

    /**
     * 修改我的社团信息
     * @param soMDTO 社团信息
     * @return 修改结果
     */
    @PostMapping(value = "/mySociety/update")
    public Result updateMySociety(@RequestBody SoMDTO soMDTO) {
        return societiesService.updateSocieties(soMDTO);
    }

    /**
     * 修改我的社团社长
     * @param newUserId 新社长id
     * @return 修改结果
     */
    @PostMapping(value = "/mySociety/change")
    public Result changeMySociety(@RequestParam("newUserId") Long newUserId) {
        return societiesService.changeMySociety(newUserId);
    }

    /**
     * 查询我的社团成员列表
     * @return 成员列表
     */
    @GetMapping(value = "/mySociety/member/list")
    public Result queryMySocietyMemberList() {
        return societiesMemberService.queryMySocietyMemberList();
    }

    /**
     * 删除我的社团成员
     * @param id 成员id
     * @return 删除结果
     */
    @PostMapping(value = "/mySociety/member/delete")
    public Result deleteMySocietyMember(@RequestParam Long id) {
        return societiesMemberService.deleteMySocietyMember(id);
    }

    /**
     * 查询我的社团入部申请列表
     * @return 入部申请列表
     */
    @GetMapping(value ="/mySociety/member/join")
    public Result queryMySocietyJoinList() {
        return societiesMemberService.queryMySocietyJoinList();
    }

    /**
     * 查询我的社团退部申请列表
     * @return 退部申请列表
     */
    @GetMapping(value = "/mySociety/member/leave")
    public Result queryMySocietyLeaveList() {
        return societiesMemberService.queryMySocietyLeaveList();
    }

    /**
     * 同意或拒绝入部申请
     * @param id 申请id
     * @param agree 同意或拒绝
     * @return 同意或拒绝结果
     */
    @PostMapping(value = "/mySociety/member/join")
    public Result agreeJoin(@RequestParam Long id, @RequestParam Boolean agree) {
        return societiesMemberService.agreeJoin(id,agree);
    }
    /**
     * 同意或拒绝退会申请
     * @param id 申请id
     * @param agree 同意或拒绝
     * @return 同意或拒绝结果
     */
    @PostMapping(value = "/mySociety/member/leave")
    public Result leaveSocieties(@RequestParam Long id, @RequestParam Boolean agree) {
        return societiesMemberService.agreeLeave(id,agree);
    }

    /**
     * 发布活动
     * @param acMDTO 活动信息
     * @return 发布结果
     */
    @PostMapping(value = "/myActivity/pub")
    public Result publishActivity(@RequestBody AcMDTO acMDTO) {
        return activitiesService.pubActivity(acMDTO);
    }

    /**
     * 查询我的活动列表
     * @return 活动列表
     */
    @GetMapping(value = "/myActivity/list")
    public Result queryMyActivityList() {
        List<Activities> acManagerId = activitiesService.query().eq("ac_manager_id", UserHolder.getUser().getUserid()).list();
        if(acManagerId.isEmpty()){
            return Result.fail("你还没有发布过活动");
        }else{
            return Result.ok(acManagerId);
        }
    }

    /**
     * 查询我的未发布活动列表
     * @return 活动列表
     */
    @GetMapping(value ="/myActivity/unpubList")
    public Result queryMyUnpubList(){
        return activitiesService.queryMyUnpubList();
    }
    /**
     * 查询我的活动评论
     * @param ac_id 活动id
     * @param state 评论状态 0:待审核 1:已通过
     * @return 评论列表
     */
    @GetMapping(value = "/myActivity/comment")
    public Result queryMyAcComment(@RequestParam("ac_id")Long ac_id,@RequestParam("state")Integer state){
        List<ActivityComment> activityComments = activityCommentService.query().eq("ac_id", ac_id).eq("state", state).list();
        if(activityComments.isEmpty()){
            return Result.fail("该活动无该类型评论");
        }else {
            return Result.ok(activityComments);
        }
    }

    /**
     * 同意或拒绝活动评论
     * @param id 评论id
     * @param agree 同意或拒绝
     * @return 同意或拒绝结果
     */
    @PostMapping(value = "/myActivity/comment/agree")
    public Result agreeMyAcComment(@RequestParam("id") Long id,
            @RequestParam(value = "agree") Boolean agree) {
        return activityCommentService.agreeMyAcComment(id,agree);
    }

    /**
     * 发布社团公告
     * @param context 公告内容
     * @return 发布结果
     */
    @PostMapping(value = "/mySociety/notice/pub")
    public Result pubNotice(@RequestParam("context") String context){
        return societyNoticeService.pubNotice(context);
    }


    /**
     * 删除社团公告
     * @param id 公告id
     * @return 删除结果
     */
    @PostMapping(value ="/mySociety/notice/delete")
    public Result deleteNotice(@RequestParam("id")Long id){
        return societyNoticeService.deleteNotice(id);
    }

    /**
     * 删除我的活动评论
     * @param id 评论id
     * @return 删除结果
     */
    @PostMapping(value = "/myActivity/comment/delete")
    public Result deleteMyAcComment(@RequestParam("id")Long id){
        return activityCommentService.deleteMyAcComment(id);
    }

}
